/**
 * @Auther: ChenforCode
 * @Date: 2018/9/13 10:22
 * @Description:
 */
package cn.chenforcode.controller;

import cn.chenforcode.entity.Book;
import cn.chenforcode.entity.BookAdmin;
import cn.chenforcode.entity.StuAdmin;
import cn.chenforcode.entity.Student;
import cn.chenforcode.service.AdminService;
import cn.chenforcode.service.BookService;
import cn.chenforcode.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private BookService bookService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private HttpSession session;

    /*------以下主要是最高权限管理员的相关控制------*/

    @RequestMapping(value = "/admin_main")
    public String admin_main(Model model) {
        List<BookAdmin> bookAdmins = adminService.getBookAdmins();
        List<StuAdmin> stuAdmins = adminService.getStuAdmins();

        model.addAttribute("bookAdmins", bookAdmins);
        model.addAttribute("stuAdmins", stuAdmins);
        return "admin_main";
    }

    @RequestMapping("/upBookAdminPrompt")
    public String updateBookAdminPromot(Model model, String id) {
        //准备工作
        model.addAttribute("bookAdmin", adminService.getBookAdmin(id));
        return "upBookAdmin";
    }

    @RequestMapping("/upStuAdminPrompt")
    public String updateStuAdminPromot(Model model, String id) {
        model.addAttribute("stuAdmin", adminService.getStuAdmin(id));
        return "upStuAdmin";
    }

    @RequestMapping("/updateBookAdmin")
    public String updateBookAdmin(BookAdmin bookAdmin) {
        adminService.upBookAdmin(bookAdmin);
        return "redirect:admin_main";
    }

    @RequestMapping("/updateStukAdmin")
    public String updateStuAdmin(StuAdmin stuAdmin) {
        adminService.upStuAdmin(stuAdmin);
        return "redirect:admin_main";
    }

    @RequestMapping("/delBookAdmin")
    public String deleteBookAdmin(String id) {
        adminService.delBookAdmin(id);
        return "redirect:admin_main";
    }

    @RequestMapping("/delStuAdmin")
    public String deleteStuAdmin(String id) {
        adminService.delStuAdmin(id);
        return "redirect:admin_main";
    }

    @RequestMapping("/addStuAdmin")
    public String addStuAdmin(StuAdmin stuAdmin) {
        String msg = adminService.addStuAdmin(stuAdmin);

        if ("success".equals(msg)) {
            if (session.getAttribute("add1_msg") != null) {
                session.removeAttribute("add1_msg");
            }
        } else {
            session.setAttribute("add1_msg", "该名称已存在");
        }
        return "redirect:admin_main";
    }

    @RequestMapping("/addBookAdmin")
    public String addBookAdmin(BookAdmin bookAdmin) {
        String msg = adminService.addBookAdmin(bookAdmin);
        if ("success".equals(msg)) {
            if (session.getAttribute("add2_msg") != null) {
                session.removeAttribute("add2_msg");
            }
        } else {
            session.setAttribute("add2_msg", "该名称已存在");
        }
        return "redirect:admin_main";
    }

    /*------以下是学生管理员的相关逻辑------*/
    @RequestMapping("/sadmin_main")
    public String sadmin_main(Model model) {
        String name = (String) session.getAttribute("uname");
        List<Student> students = studentService.getAllStudents(name);
        model.addAttribute("students", students);
        return "stuAdmin_main";
    }

    @RequestMapping("/addStudent")
    public String addStudent(Student student) {
        String name = (String) session.getAttribute("uname");
        String msg = studentService.addStudent(student, name);

        if ("success".equals(msg)) {
            if (session.getAttribute("addstu_msg") != null) {
                session.removeAttribute("addstu_msg");
            }
        } else {
            session.setAttribute("addstu_msg", "该名称已存在");
        }
        return "redirect:sadmin_main";
    }

    @RequestMapping("/upStudentPro")
    public String upStudentPro(Model model, String id) {
        String name = (String) session.getAttribute("uname");
        //将这个学生修改前的信息保存以便于使用
        model.addAttribute("student", studentService.getStudent(id, name));
        return "upStudent";
    }

    @RequestMapping("/upStudent")
    public String upStudent(Student student) {
        String name = (String) session.getAttribute("uname");
        studentService.updateStudet(student, name);
        return "redirect:sadmin_main";
    }

    @RequestMapping("/delStudent")
    public String deleteStudent(String id) {
        String name = (String) session.getAttribute("uname");
        String msg = studentService.deleteStudent(id, name);
        if ("success".equals(msg)) {
            if (session.getAttribute("delstu_msg") != null) {
                session.removeAttribute("delstu_msg");
            }
        } else {
            session.setAttribute("delstu_msg", "该学生不能被删除");
        }
        return "redirect:sadmin_main";
    }

    /*---以下是图书管理员的相关控制---*/
    @RequestMapping("/badmin_main")
    public String badmin_main(Model model) {
        String name = (String) session.getAttribute("uname");
        List<Book> books = bookService.getAllBooks(name);
        model.addAttribute("books", books);
        return "bookAdmin_main";
    }

    @RequestMapping("/addBook")
    public String addBook(Book book) {
        String name = (String) session.getAttribute("uname");
        String msg = bookService.addBook(book, name);

        if ("success".equals(msg)) {
            if (session.getAttribute("addbook_msg") != null) {
                session.removeAttribute("addbook_msg");
            }
        } else {
            session.setAttribute("addbook_msg", "该名称已存在");
        }
        return "redirect:badmin_main";
    }

    @RequestMapping("/upBookPro")
    public String upBookPro(Model model, String id) {
        String name = (String) session.getAttribute("uname");
        //将这个学生修改前的信息保存以便于使用
        model.addAttribute("book", bookService.getBook(id, name));
        return "upBook";
    }

    @RequestMapping("/upBook")
    public String upBook(Book book) {
        String name = (String) session.getAttribute("uname");
        bookService.updateBook(book, name);
        return "redirect:badmin_main";
    }

    @RequestMapping("/delBook")
    public String deleteBook(String id) {
        String name = (String) session.getAttribute("uname");
        String msg = bookService.deleteBook(id, name);
        if ("success".equals(msg)) {
            if (session.getAttribute("delbook_msg") != null) {
                session.removeAttribute("delbook_msg");
            }
        } else {
            session.setAttribute("delbook_msg", "该书籍不能被删除");
        }
        return "redirect:badmin_main";
    }

    /*---以下是项目版权信息---*/

    @RequestMapping("/copyright")
    public String viewCopyright() {
        return "copyright";
    }
}
